package com.onlygx.dva.web.base.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.onlygx.dva.common.exception.FailRequestException;
import com.onlygx.dva.common.properties.CommonCoreProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


/**
 * 瞳见科技
 *
 * @author : GaoXiang
 * 时间 : 2022/1/5
 * 介绍 :
 */
@Service
@RequiredArgsConstructor
public class PublicService {

    //final RedisService redisService;

    final CommonCoreProperties coreProperties;

/*    public boolean phoneCodeVerify(String userId, String code) {
        if (StrUtil.isEmpty(code)) {
            throw new FailRequestException("验证码不为空");
        }

        Object pCode = Optional.ofNullable(redisService.get(CacheConstant.PHONE_CODE + userId)).orElseThrow(() -> new FailRequestException("验证码已过期"));
        return pCode.equals(code);
    }*/

    public void savePhoneCode(String userId, String code) {
        if (StrUtil.isEmpty(code)) {
            throw new FailRequestException("验证码不为空");
        }
        //redisService.set(CacheConstant.PHONE_CODE + userId, code, 5, TimeUnit.MINUTES);
    }

    public String decryptStrByAES(String info) {
        //加载秘钥
        AES aes = SecureUtil.aes(coreProperties.getSecret().getBytes());
        //解密
        return aes.decryptStr(info);
    }

    public String encryptHexByAES(String content) {
        //加载秘钥
        AES aes = SecureUtil.aes(coreProperties.getSecret().getBytes());
        //解密
        return aes.encryptHex(content);
    }
}
